package com.kim.smartring.activity;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.kim.smartring.R;
import com.kim.smartring.db.dao.UserDao;
import com.kim.smartring.db.model.User;
import com.kim.smartring.net.business.LoginBusiness;
import com.kim.smartring.net.business.LoginBusiness.LoginCallback;
import com.kim.smartring.utils.StringUtils;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

	private static final int MSG_ACCOUNT_RESUME = 0x10001;
	private static final int MSG_LOGIN_SUCCESS = 0x10002;
	private static final int MSG_LOGIN_FAIL = 0x10003;

	private EditText edtAccount;
	private EditText edtPassword;

	private Button btnLogin;
	private Button btnCancel;

	private String account;
	private String password;

	@SuppressLint("HandlerLeak")
	private Handler loginHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_ACCOUNT_RESUME:
				resumeAccountInfo();
				break;
			case MSG_LOGIN_SUCCESS:
				break;
			case MSG_LOGIN_FAIL:
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		initParam();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			doLogin();
			break;
		case R.id.btn_cancel:
			finish();
			break;
		}
	}

	private void initParam() { // 获取用户账号信息
		loginHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				UserDao userDao = new UserDao(getApplicationContext());
				ArrayList<User> users = (ArrayList<User>) userDao
						.findAll(User.class);
				if (users != null && users.size() > 0) {
					User user = users.get(0);
					account = user.getAccount();
					password = user.getPassword();
					loginHandler.sendEmptyMessage(MSG_ACCOUNT_RESUME);
				}
			}
		}, 200);
	}

	private void initView() {
		edtAccount = (EditText) findViewById(R.id.edt_account);
		edtPassword = (EditText) findViewById(R.id.edt_password);

		btnLogin = (Button) findViewById(R.id.btn_login);
		btnCancel = (Button) findViewById(R.id.btn_cancel);

		btnLogin.setOnClickListener(this);
		btnCancel.setOnClickListener(this);

	}

	private void resumeAccountInfo() {
		edtAccount.setText(account);
		edtPassword.setText(password);
	}

	private void doLogin() {

		account = edtAccount.getText().toString();
		password = edtPassword.getText().toString();

		if (StringUtils.isEmpty(account)) {
			Toast.makeText(this, "请输入账户", Toast.LENGTH_SHORT).show();
			return;
		}

		if (StringUtils.isEmpty(password)) {
			Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
			return;
		}

		User user = new User();
		user.setAccount(account);
		user.setPassword(password);

		LoginBusiness.login(this, user, new LoginCallback() {

			@Override
			public void loginSuccess() {
				loginHandler.sendEmptyMessage(MSG_LOGIN_SUCCESS);
			}

			@Override
			public void loginFail() {
				loginHandler.sendEmptyMessage(MSG_LOGIN_FAIL);
			}
		});
	}

}
